<template>
  <div class="shopCar">
    <div class="shop-Car-top">新用户下单立减5元</div>
    <div class="shop-Car-title">
      <h4>购物车</h4>
      <span>清空购物车</span>
    </div>
    <div v-for="child in shopCarList" :key="child.id" class="shop-Car_box">
      <img
        :src="child.imgUrl"
        style="width: 60px; height: 60px; border-radius: 5px"
      />
      <div class="box-info">
        <span class="name">{{ child.name }}</span>
        <span class="ingredient">{{ child.ingredient }}</span>
        <span class="price">￥{{ getprice }}</span>
      </div>
      <div style="display: flex; align-items: center">
        <div
          class="plus"
          @click="changeNum(child.id, -1)"
          v-show="child.num >= 1"
        >
          -
        </div>
        <span v-show="child.num >= 1">{{ child.num }}</span>
        <div class="minus" @click="changeNum(child.id, 1)">+</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    // 改变购物车的商品数量
    changeNum(id, num) {
      // 把值传到vuex中
      this.$store.commit("changeNum", { id, num });
    },
  },
  computed: {
    shopCarList() {
      return this.$store.getters.shopCarList;
    },
    // 计算价格
    getprice() {
      let total = 0;
      for (let obj of this.shopCarList) {
        total += obj.num * obj.price;
      }
      return parseFloat(total).toFixed(2);
    },
  },
};
</script>

<style lang="less" scoped>
.shopCar {
  width: 98;
  padding-bottom: 40px;
  .shop-Car-top {
    background-color: #fff8e1;
    height: 30px;
    width: 100%;
    border-radius: 30px 30px 0 0;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
  }
  .shop-Car-title {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    padding-bottom: 10px;
    h4 {
      padding: 10px 0 0 14px;
    }
    span {
      padding: 10px 14px 0 0;
      color: #6666;
    }
  }
  .shop-Car_box {
    margin: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center; //侧轴上（x轴）居中对齐
    .box-info {
      display: flex;
      flex: 1;
      flex-direction: column;
      padding-left: 10px;
      .name {
        font-size: 12px;
      }
      .ingredient {
        font-size: 12px;
      }
      .price {
        color: #fb4e44;
      }
    }
    .plus {
      height: 20px;
      width: 20px;
      border-radius: 4px;
      font-size: 20px;
      text-align: center;
      line-height: 18px;
      border: 1px solid #ffcc33;
      background-color: #fff;
      margin: 0 10px;
      font-weight: 600;
    }
    .minus {
      height: 20px;
      width: 20px;
      border-radius: 4px;
      font-size: 20px;
      text-align: center;
      line-height: 18px;
      border: 1px solid #ffcc33;
      background-color: #ffcc33;
      margin: 0 10px;
      font-weight: 600;
    }
  }
}
</style>